<?php
// +----------------------------------------------------------------------
// | ThinkBLOG (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author nicholas <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: Sessions.class.php 5 2011-09-16 10:26:11Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class Sessions {
	
	public $db = NULL;
	public $sid = NULL;
	public $var;
	public $isnew = false;
	public $newguest = array ('sid' => 0, 'ip1' => 0, 'ip2' => 0, 'ip3' => 0, 'ip4' => 0, 'userid' => 0, 'username' => '', 'roleid' => 5, 'group' => 0, 'module' => 0, 'action' => 0, 'postid' => 0, 'lastactivity' => 0, 'lastolupdate' => 0 );
	
	public $old = array ('sid' => '', 'ip' => '', 'userid' => 0 );
	
	public function __construct($sid = '', $ip = '', $userid = 0) {
		$this->db = D ( 'Sessions' );
		$this->old = array ('sid' => $sid, 'ip' => $ip, 'userid' => $userid );
		$this->var = $this->newguest;
		if (! empty ( $ip )) {
			$this->init ( $sid, $ip, $userid );
		}
	}
	
	public function set($key, $value) {
		if (isset ( $this->newguest [$key] ))
			$this->var [$key] = $value;
		elseif ($key == 'ip') {
			$ips = explode ( '.', $value );
			$this->set ( 'ip1', $ips [0] );
			$this->set ( 'ip2', $ips [1] );
			$this->set ( 'ip3', $ips [2] );
			$this->set ( 'ip4', $ips [3] );
		}
	}
	
	public function get($key) {
		if (isset ( $this->newguest [$key] )) {
			return $this->var [$key];
		} elseif ($key == 'ip') {
			return $this->get ( 'ip1' ) . '.' . $this->get ( 'ip2' ) . '.' . $this->get ( 'ip3' ) . '.' . $this->get ( 'ip4' );
		}
	}
	
	public function init($sid, $ip, $userid) {
		$this->old = array ('sid' => $sid, 'ip' => $ip, 'userid' => $userid );
		
		$session = array ();
		if ($sid) {
			$session = $this->db->where ( "sid='$sid' AND CONCAT_WS('.', ip1,ip2,ip3,ip4)='$ip'" )->find ();
		}
		
		if (empty ( $session ) || $session ['userid'] != $userid) {
			$session = $this->create ( $ip, $userid );
		}
		
		$this->var = $session;
		$this->sid = $session ['sid'];
	}
	
	public function create($ip, $userid) {
		$this->isnew = true;
		$this->var = $this->newguest;
		$this->set ( 'sid', trandom ( 6 ) );
		$this->set ( 'userid', $userid );
		$this->set ( 'ip', $ip );
		$this->set ( 'lastactivity', time () );
		$this->sid = $this->var ['sid'];
		return $this->var;
	}
	
	public function delete() {
		$onlinehold = 15;
		$guestspan = 60;
		
		$onlinehold = time () - $onlinehold;
		$guestspan = time () - $guestspan;
		
		$condition = " sid='{$this->sid}' ";
		$condition .= " OR lastactivity<$onlinehold ";
		$condition .= " OR (userid='0' AND ip1='{$this->var['ip1']}' AND ip2='{$this->var['ip2']}' AND ip3='{$this->var['ip3']}' AND ip4='{$this->var['ip4']}' AND lastactivity>$guestspan) ";
		$condition .= $this->var ['userid'] ? " OR (userid='{$this->var['userid']}') " : '';
		$this->db->where ( $condition )->delete ();
	}
	
	public function update() {
		global $_T;
		if ($this->sid !== NULL) {
			$data = taddslashes ( $this->var );
			if ($this->isnew) {
				$this->delete ();
				$this->db->add ( $data );
			} else {
				$this->db->save ( $data );
			}
			$_T ['session'] = $data;
			cookie ( 'sid', $this->sid, 86400 );
		}
	}
	
	public function getOnlineCount($type = 0) {
		$condition = $type == 1 ? ' userid>0 ' : '';
		return $this->db->where ( $condition )->count ();
	}

}